package xuhaijia.lanou3g.autohome.forum;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             


import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.StringValues;
import xuhaijia.lanou3g.autohome.forum.hotinvitation.HotInvitationFragment;
import xuhaijia.lanou3g.autohome.forum.normalforum.NormalForumFragment;

/**
 * Created by dllo on 16/8/15.
 */
public class ForumFragment extends BaseFragment implements TabLayout.OnTabSelectedListener {

    private PrimaryAdapter primaryAdapter;
    private ViewPager primaryVp;
    private TabLayout primaryTb;
    private ArrayList<String> tbArrayLists;
    private ArrayList<Fragment> fragments;
    public DrawerLayout drawerLayout;
    private RelativeLayout rightMenuLayout;
    private int listSelect;
    private View drawableView;

    private RightMenuAdapter rightMenuAdapter;
    private JxRecommendFragment jxRecommendFragment;

    @Override
    protected int setLayout() {
        return R.layout.fragment_primary;
    }

    @Override
    protected void initView() {
        primaryAdapter = new PrimaryAdapter(getChildFragmentManager());
        primaryVp = bindView(R.id.primary_vp);
        primaryTb = bindView(R.id.primary_tb);
        rightMenuLayout = bindView(R.id.main_right_drawer_layout);
        drawerLayout = bindView(R.id.main_drawer_layout);
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        //设置透明
//        drawerLayout.setScrimColor(0x00000000);

    }

    @Override
    protected void initData() {
        tbArrayLists = new ArrayList<>();
        tbArrayLists.add("精选推荐");
        tbArrayLists.add("热帖");
        tbArrayLists.add("常用论坛");
        fragments = new ArrayList<>();
        jxRecommendFragment = new JxRecommendFragment();
        jxRecommendFragment.setFather(this);
        fragments.add(jxRecommendFragment);
        fragments.add(new HotInvitationFragment());
        fragments.add(new NormalForumFragment());
        primaryAdapter.setFragments(fragments);
        primaryAdapter.setTbArrayLists(tbArrayLists);
        primaryVp.setAdapter(primaryAdapter);
        primaryTb.setupWithViewPager(primaryVp);
        primaryTb.setTabTextColors(Color.BLACK, Color.BLUE);
        primaryTb.setSelectedTabIndicatorColor(Color.BLUE);
        primaryTb.addOnTabSelectedListener(this);
        // 获取屏幕宽高
//        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//        DisplayMetrics dm = new DisplayMetrics();
//        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//        DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) rightMenuLayout.getLayoutParams();
//        layoutParams.height = dm.heightPixels;
//        rightMenuLayout.setLayoutParams(layoutParams);

    }

    @Override
    public void load() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void onResume() {
        super.onResume();
        initDrawable();
        addDrawable();
    }

    private void addDrawable() {
        rightMenuLayout.addView(drawableView);

    }

    private void removeDrawable() {
        rightMenuLayout.removeView(drawableView);
    }

    private void initDrawable() {
        drawableView = LayoutInflater.from(getContext()).inflate(R.layout.right_drawer_layout, null);
        ListView listView = (ListView) drawableView.findViewById(R.id.right_drawer_lv);
        TextView textView = (TextView) drawableView.findViewById(R.id.right_drawer_close);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openRightLayout();
            }
        });
        rightMenuAdapter = new RightMenuAdapter(mContext);
        rightMenuAdapter.setType(StringValues.strs);
        listView.setAdapter(rightMenuAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jxRecommendFragment.setLocation(position);
                openRightLayout();
            }
        });

    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (tab.getPosition() == 0) {
            addDrawable();
        } else {

            removeDrawable();
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }


    // 消息开关事件
    public void openRightLayout() {
        if (drawerLayout.isDrawerOpen(rightMenuLayout)) {
            drawerLayout.closeDrawer(rightMenuLayout);
        } else {
            drawerLayout.openDrawer(rightMenuLayout);
        }
    }

    public void setListSelect(int listSelect) {
        this.listSelect = listSelect;
        rightMenuAdapter.setSelectPos(listSelect);//文字变色
    }


}
